Intersection of two linked lists

Time: O(M+N); Space: O(1); easy

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

begin to intersect at node c1.

Example 1:

~Input: intersectVal = 8, headA = [4,1,8,4,5], headB = [5,0,1,8,4,5], skipA = 2, skipB = 3~

Input: headA = [2,4,6,7,8], headB = [1,3,5,6,7,8]

Output: 6

Input Explanation:

  • The intersected node’s value is 8 (note that this must not be 0 if the two lists intersect).

  • From the head of A, it reads as [4,1,8,4,5].

  • From the head of B, it reads as [5,0,1,8,4,5].

  • There are 2 nodes before the intersected node in A;

  • There are 3 nodes before the intersected node in B.

Example 2:

Input: intersectVal = 2, headA = [0,9,1,2,4], headB = [3,2,4], skipA = 3, skipB = 1

Output: 2

Input Explanation:

  • The intersected node’s value is 2 (note that this must not be 0 if the two lists intersect).

  • From the head of A, it reads as [0,9,1,2,4].

  • From the head of B, it reads as [3,2,4].

  • There are 3 nodes before the intersected node in A;

  • There are 1 node before the intersected node in B.

Example 3:

Input: intersectVal = 0, headA = [2,6,4], headB = [1,5], skipA = 3, skipB = 2

Output: None

Input Explanation:

  • From the head of A, it reads as [2,6,4].

  • From the head of B, it reads as [1,5].

  • Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.

Explanation:

  • The two lists do not intersect, so return null.

Notes:

  • If the two linked lists have no intersection at all, return null.

  • The linked lists must retain their original structure after the function returns.

  • You may assume there are no cycles anywhere in the entire linked structure.

  • Your code should preferably run in O(n) time and use only O(1) memory.

1. Brute Force [O(MxN), O(1)]

For each node ai in list A, traverse the entire list B and check if any node in list B coincides with Ai.

2. Hash Table [O(M+N), O(M) or O(N)]

Traverse list A and store the address / reference to each node in a hash set.

Then check every node Bi in list B: if Bi appears in the hash set, then Bi is the intersection node.

3. Two Pointers [O(M+N), O(1)]

Maintain two pointers pA and pB initialized at the head of A and B, respectively. Then let them both traverse through the lists, one node at a time.

When pA reaches the end of a list, then redirect it to the head of B (yes, B, that’s right.); similarly when pB reaches the end of a list, redirect it the head of A.

If at any point pA meets pB, then pA/pB is the intersection node.

To see why the above trick would work, consider the following two lists:

A = {1,3,5,7,9,11} and B = {2,4,9,11}, which are intersected at node ‘9’.

Since len(B) (=4) < len(A) (=6), pB would reach the end of the merged list first, because pB traverses exactly 2 nodes less than pA does. By redirecting pB to head A, and pA to head B, we now ask pB to travel exactly 2 more nodes than pA would. So in the second iteration, they are guaranteed to reach the intersection node at the same time.

If two lists have intersection, then their last nodes must be the same one. So when pA/pB reaches the end of a list, record the last element of A/B respectively. If the two last elements are not the same one, then the two lists have no intersections.

[11]:
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
[15]:
class Solution1(object):
    """
    Time: O(M+N)
    Space: O(1)
    """
    def getIntersectionNode(self, headA, headB):
        """
        :type headA: ListNode
        :type headB: ListNode
        :rtype: ListNode
        """
        curA, curB = headA, headB
        begin, tailA, tailB = None, None, None

        while curA and curB:
            if curA.val == curB.val:         # compare values, not objects
                begin = curA
                break

            if curA.next:
                curA = curA.next
            elif tailA is None:
                tailA = curA
                # print("Redirecting curA to head B")
                curA = headB
            else:
                break

            if curB.next:
                curB = curB.next
            elif tailB is None:
                tailB = curB
                # print("Redirecting curB to head A")
                curB = headA
            else:
                break

        return begin
[16]:
s = Solution1()

headA = ListNode(2)
headA.next = ListNode(4)
headA.next.next = ListNode(6)
headA.next.next.next = ListNode(7)
headA.next.next.next.next = ListNode(8)
headB = ListNode(1)
headB.next = ListNode(3)
headB.next.next = ListNode(5)
headB.next.next.next = ListNode(6)
headB.next.next.next.next = ListNode(7)
headB.next.next.next.next.next = ListNode(8)
res = s.getIntersectionNode(headA, headB)
assert res.val == 6

headA = ListNode(0)
headA.next = ListNode(9)
headA.next.next = ListNode(1)
headA.next.next.next = ListNode(2)
headA.next.next.next.next = ListNode(4)
headB = ListNode(3)
headB.next = ListNode(2)
headB.next.next = ListNode(4)
res = s.getIntersectionNode(headA, headB)
assert res.val == 2

headA = ListNode(2)
headA.next = ListNode(4)
headA.next.next = ListNode(6)
headB = ListNode(1)
headB.next = ListNode(5)
res = s.getIntersectionNode(headA, headB)
assert res == None